#include "geminai_power.h"
#include "geminai_usbh_modem_board.h"

#define POWERPIN  GPIO_NUM_3 // 采样的引脚
#define POWERMODEPRIORITY  15 //设定任务的优先级

#define POWERMODELIMIT_SLEEP    6 // 低于一个值 就认定为断电 进入低功耗模式
#define POWERMODELIMIT_WAKE     19 // 高于某一个值 就认定为离开低功耗模式
#define POWERMODEWAKETIME 30 * 1000000 // 用来定时唤醒ESP32  30S



static const char *TAG = "geminai_power";
//double _24V_INT_Read(void);             //获取24V输入电压

uint8_t is_adapter_exception = 0 ; // 用于判读当前适配器是否异常（6~19v）


void setup_sleep_and_wakeup_task()
{
    esp_sleep_enable_timer_wakeup(POWERMODEWAKETIME); // 定时唤醒 24小时 时间可调整
    esp_sleep_enable_ext0_wakeup(POWERPIN, 1);
    while (1)
    {
        double voltage = _24V_INT_Read();
        if (voltage <= POWERMODELIMIT_WAKE)
        {
            if (voltage < POWERMODELIMIT_SLEEP)
            {
            
                GEMINAI_LOGI(TAG,"gooooooooooooooooooooooo to sleep");
                All_gpio_down();
                save_frig_para();
                esp_deep_sleep_start();
            }
            is_adapter_exception = 1;
        }
        else
        {
            is_adapter_exception = 0;
        }

        // 用于测试是否进入休眠模式
        // GEMINAI_LOGD(TAG,"device is awake");
        // GEMINAI_LOGD(TAG,"is waking real = %d", is_waking_real);

        vTaskDelay(pdMS_TO_TICKS(3000)); // 使用 FreeRTOS 的延迟函数 3秒查看一次 // 使用 FreeRTOS 的延迟函数   3s看一次
    }
}



void creat_lowpowermode_task()
{
    xTaskCreate(setup_sleep_and_wakeup_task,"setup_sleep_and_wakeup_task",3072,NULL,5,NULL);
}


// 2024 10 9 将代码进入低功耗 电流保持位50uA 后续有需求在加入其他功耗功能
void test_power()
{
    esp_deep_sleep_start();
}

//上电检测是否由定时器启动
void boot_check(void)
{
    long long  wakeTime = 0;
    int i=0;

    esp_sleep_wakeup_cause_t wakeup_reason = esp_sleep_get_wakeup_cause();
    printf("***************wakeup cause = %d\n", wakeup_reason);
    if(wakeup_reason == ESP_SLEEP_WAKEUP_TIMER)
    {
        int wakeup_count = timer_wakeup_count_get();
        wakeTime = (wakeup_count < 16) ? (24 * 60 * 1000) : (7 * 24 * 60 * 1000); // 24小时或7天
        timer_wakeup_count_set(wakeup_count + 1); // 设置定时器唤醒次数

        POW4G_EN_Init();
        POW3V3_EN_Init();
        POW4G_EN_ON;
        modem_lte_task_start();// 启动4G任务
        //发送定位事件
        vTaskDelay(8000 / portTICK_PERIOD_MS);//等待网络初始化
        send_shutdown_base_station_event();// 发送定位事件
        while(!uxQueueMessagesWaiting(mqtt_queue))
        {
            printf("whit send message\n");
            if(i++ > 60)
            {
                printf("tiemout go to sleep\n");
                break;
            }
            vTaskDelay(1000 / portTICK_PERIOD_MS);
            if (_24V_INT_Read() > POWERMODELIMIT_WAKE )
            {
                printf("24v OK wake up \n");
                esp_restart();
            }
        }
        vTaskDelay(5000 / portTICK_PERIOD_MS);
        esp_sleep_enable_timer_wakeup(wakeTime * 1000); // 定时唤醒
        esp_sleep_enable_ext0_wakeup(POWERPIN, 1);// 外部唤醒
        POW4G_EN_OFF; 
        printf("go to000000000000000000000000000000000000 sleep\n");
        esp_deep_sleep_start();

    }
    else
        timer_wakeup_count_set(0);// 清除定时器唤醒次数
}


